package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.Administrators;
import com.dyypdadmin.pojo.Users;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;
    /*
    * 微信用户
    * 1.用户注册
    * 2.用户登录
    * 3.获取用户列表
    * */
    /*
    * 1.用户注册
    * */
    @PostMapping("/register")
    public Result userRegister(@RequestBody Map<String,Object> requestBody){
        String username = (String) requestBody.get("username");
        String phone = (String) requestBody.get("phone");
        String password = (String) requestBody.get("password");
        String address = (String) requestBody.get("address");
        int result = usersService.addUser(username,phone,password,address);
        if(result>0){
            return new Result(true,"注册成功");
        }else{
            return new Result(false,"注册失败");
        }

    }
    /*
     * 2.用户登录
     * */
    @PostMapping("/login")
    public Result login(@RequestBody Map<String,Object> requestBody){
        String username = (String) requestBody.get("username");
        String password = (String) requestBody.get("password");
        Users users = usersService.getUserByUsernameAndPassword(username,password);
        if(users!=null){
            return new Result(true,users,"登录成功");
        }else{
            return new Result(false,"登录失败，请重新尝试");
        }

    }

    /*
    * 3.获取用户列表
    *
    * */
    @GetMapping("/getAll")
    public Result getUsersList(){
        List<Users> usersList = usersService.getAllUser();
        if(usersList!=null){
            return new Result(true,usersList,"获取用户列表成功");
        }else{
            return new Result(true,"访问到了接口，数据库中无数据");
        }
    }
    /**
     * @description:更新用户数据
     * @author:邓盈盈
     * @date: 2024/3/8 下午 05:35
     * @param:
     * @return:
     **/
    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody Users users){
        int result = usersService.updateUserInfo(users);
        if(result>0){
            return new Result(true,"success");
        }else{
            return new Result(false,"fail");
        }

    }
    /**
     * @description:通过用户名查询用户信息
     * @author:邓盈盈
     * @date: 2024/3/8 下午 05:47
     * @param:
     * @return:
     **/
    @GetMapping("/getUserInfoByUsername/{username}")
    public Result getUserInfoByUsername(@PathVariable("username")String username){
        List<Users> result = usersService.getUserInfoByUsername(username);
        if(result!=null){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }

    }
    /**
     * @description:系统注册使用人数
     * @author:
     * @date: 2024/3/8 下午 08:13
     * @param: 
     * @return: 
     **/
    @GetMapping("/getUserCounts")
    public Result getUserCounts(){
        int result = usersService.getUserCounts();
        return new Result(true,result);
    
    }
    /**
     * @description: 用户修改密码
     * @author:
     * @date: 2024/3/31 上午 01:26
     * @param:
     * @return:
     **/
    @PutMapping("/updatePwdByUserId")
    public Result updatePwdByUserId(@RequestBody Users users){
        int result = usersService.updatePwdByUserId(users);
        if(result>0){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }

    }
}
